# 输入检查号页面
#
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QLineEdit, qApp, QDesktopWidget, QLabel, QPushButton, QMessageBox
from PyQt5.QtGui import QIcon, QFont,QKeyEvent
from PyQt5.QtCore import Qt
from age import Age
from sqlite import insert
import traceback

class Example(QMainWindow):
    def __init__(self, jianchahao=''):
        super().__init__()
        global jianchahao_text
        jianchahao_text = jianchahao
        self.initUI()

    def initUI(self):
        #生成日期,做为唯一识别码
        import datetime
        now = datetime.datetime.now()
        self.date_str =now.strftime('%Y-%m-%d %H:%M')
        ####
        exitAction = QAction(QIcon('images/exit.png'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(qApp.quit)

        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)

        lbl1 = QLabel('检查号', self)
        lbl1.setFont(QFont('SansSerif', 24))
        lbl1.move(200, 50)

        global line
        line = QLineEdit(self)
        line.setText(jianchahao_text)
        line.setFont(QFont('Arial Black', 24))
        line.resize(150, 50)
        line.move(180, 150)

        btn = QPushButton('返回', self)
        btn.setFont(QFont('SansSerif', 14))
        btn.resize(100, 60)
        btn.move(100, 400)
        btn.clicked.connect(self.on_back_clicked)

        btn_next = QPushButton('下一步', self)
        btn_next.setFont(QFont('SansSerif', 14))
        btn_next.resize(100, 60)
        btn_next.move(300, 400)
        btn_next.clicked.connect(self.on_next_clicked)

        self.setFixedSize(500, 500)
        self.setWindowTitle('玉环县人民医院 TW3骨龄法 软件')
        self.setWindowIcon(QIcon("images/myicon.png"))
        self.center()
        self.show()

    # 点击返回按钮的时候 引入主界面并将自己销毁
    def on_back_clicked(self):
        from main import window
        self.window().destroy()
        self.diag = window()
        self.diag.show()

    def on_next_clicked(self):
        text = line.text()
        # 若为空则返回焦点到输入框
        if text != '':
            insert(text,self.date_str,'TIME','string')
            self.diag = Age(text,'')
            self.diag.show()
            self.window().hide()
        else:
            line.setFocus()
            return 0

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def keyPressEvent(self, event):
        keyEvent = QKeyEvent(event)
        if (keyEvent.key() == Qt.Key_Enter) or (keyEvent.key() == Qt.Key_Return):
            self.on_next_clicked()

    def closeEvent(self, QCloseEvent):
        reply = QMessageBox.question(self, '退出？', "确定退出吗?",
                                     QMessageBox.Yes, QMessageBox.No)
        if reply == QMessageBox.Yes:
            qApp.quit
        else:
            QCloseEvent.ignore()
# if __name__ == '__main__':
#     app = QApplication(sys.argv)
#     st = Example()
#     sys.exit(app.exec_())
